package me.zxk.qwert_poetry.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import me.zxk.qwert_poetry.dao.User;
import me.zxk.qwert_poetry.dto.LikePoemDTO;
import me.zxk.qwert_poetry.dao.Poem;
import me.zxk.qwert_poetry.dto.PageResult;
import me.zxk.qwert_poetry.dto.PoemDTO;
import me.zxk.qwert_poetry.dto.response.PoemDetailResponseDTO;
import me.zxk.qwert_poetry.dto.response.PoemSimpleResponseDTO;
import me.zxk.qwert_poetry.dto.response.Response;
import me.zxk.qwert_poetry.service.PoemService;
import me.zxk.qwert_poetry.util.UserContextHolder;
import me.zxk.qwert_poetry.util.VerifyToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "诗词接口")
@RestController
public class PoemController {
    @Autowired
    private PoemService poemService;

    @Operation(summary = "根据id获取诗词")
    @GetMapping("/api/poems/get")
    public Response<PoemDetailResponseDTO> getPoemById(@RequestParam int id) {
        Poem p = poemService.getPoemById(id);
        PoemDetailResponseDTO dto = PoemDetailResponseDTO.fromEntity(p, poemService);
        return Response.ok(dto);
    }

    @Operation(summary = "添加诗词")
    @PostMapping("/api/poems/add")
    @VerifyToken(needAdmin = true)
    public Response<Poem> addPoem(@RequestBody PoemDTO poem) {
        return Response.ok(poemService.addPoem(poem));
    }

    @Operation(summary = "编辑诗词")
    @PostMapping("/api/poems/edit")
    @VerifyToken(needAdmin = true)
    public Response<Poem> editPoem(@RequestBody PoemDTO poem) {
        return Response.ok(poemService.editPoem(poem));
    }

    @Operation(summary = "删除诗词")
    @PostMapping("/api/poems/delete")
    @VerifyToken(needAdmin = true)
    public Response<Boolean> deletePoem(@RequestBody PoemDTO dto) {
        return Response.ok(poemService.deletePoem(dto.getId()));
    }

    @Operation(summary = "列出所有诗词")
    @GetMapping("/api/poems/list")
    public Response<PageResult<PoemDetailResponseDTO>> listPoems(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size
    ){
        return Response.ok(new PageResult<>(poemService.listPoems(page, size).map(p -> PoemDetailResponseDTO.fromEntity(p, poemService))));
    }

    @Operation(summary = "按作者列出诗词")
    @GetMapping("/api/poems/list/author")
    public Response<PageResult<PoemSimpleResponseDTO>> listPoemsByAuthorId(
            @RequestParam int authorId,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size
    ){
        return Response.ok(new PageResult<>(poemService.listPoemsByAuthorId(authorId, page, size).map(p -> PoemSimpleResponseDTO.fromEntity(p, poemService))));
    }

    @Operation(summary = "按关键词搜索诗词", description = "搜索诗词内容、标题、作者")
    @GetMapping("/api/poems/search/keyword")
    public Response<PageResult<PoemDetailResponseDTO>> searchPoems(
            @RequestParam(defaultValue = "") String keyword,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size
    ){
        return Response.ok(new PageResult<>(poemService.searchPoemsByKeyword(keyword, page, size).map(p -> PoemDetailResponseDTO.fromEntity(p, poemService))));
    }

    @Operation(summary = "按标签搜索诗词")
    @GetMapping("/api/poems/list/tag")
    public Response<PageResult<PoemDetailResponseDTO>> listPoemsByTag(
            @RequestParam int tagId,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size
    ){
        return Response.ok(new PageResult<>(poemService.searchPoemsByTag(tagId, page, size).map(p -> PoemDetailResponseDTO.fromEntity(p, poemService))));
    }

    @Operation(summary = "点赞诗词", description = "点赞或取消点赞诗词。返回是否点赞成功")
    @PostMapping("/api/poems/like")
    @VerifyToken
    public Response<Boolean> likePoem(@RequestBody LikePoemDTO dto) {
        User u = UserContextHolder.getUserContext().getUser();
        return Response.ok(poemService.likePoem(dto.getPoemId(), u.getId(), dto.getLiked()));
    }

}
